﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QUANLIHOCSINH.BussinessObject;
using QUANLIHOCSINH.BussinessLogic;
namespace QUANLIHOCSINH
{
    enum Status_QuanLiPhanLop { View, Insert, Update, Delete };
    public partial class FrmPhanLop : Form
    {
        Status_QuanLiPhanLop TrangThai = Status_QuanLiPhanLop.View;
        HOCSINHBUS hocsinh = new HOCSINHBUS();
        LOPBUS lop = new LOPBUS();
        KHOIBUS khoi = new KHOIBUS();
        NAMHOCBUS namhoc = new NAMHOCBUS();
        PHANLOPBUS phanlop = new PHANLOPBUS();
        BANGDIEMBUS bangdiem = new BANGDIEMBUS();
        public FrmPhanLop()
        {
            InitializeComponent();
        }
        private void EnableButton(bool Enable)
        {
            btnThem.Visible = !Enable;
            btnSua.Visible = !Enable;
            btnXoa.Visible = !Enable;
            btnCapNhat.Visible = Enable;
            btnHuy.Visible = Enable;
            gbphanlop.Enabled = Enable;
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                PHANLOP hs = new PHANLOP();
                int r = dgvPhanLop.CurrentCell.RowIndex;
                hs.MaHocSinh = dgvPhanLop.Rows[r].Cells[3].Value.ToString();
                hs.MaLop = dgvPhanLop.Rows[r].Cells[2].Value.ToString();
                hs.MaKhoi = dgvPhanLop.Rows[r].Cells[1].Value.ToString();
                hs.MaNamHoc = dgvPhanLop.Rows[r].Cells[0].Value.ToString();
                phanlop.XoaPhanLop(hs);
                dgvPhanLop.DataSource = phanlop.LayDanhSachPhanLop(cbNamHoc.SelectedValue.ToString(), cbKhoi.SelectedValue.ToString());
                FrmPhanLop_Load(sender, e);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            int r = dgvPhanLop.CurrentCell.RowIndex;
            cbLop.SelectedValue = dgvPhanLop.Rows[r].Cells[2].Value.ToString();

            this.cbHocSinh.DataSource = hocsinh.LayDanhSachHocSinh();
            this.cbHocSinh.DisplayMember = "TenHocSinh";
            this.cbHocSinh.ValueMember = "MaHocSinh";

            cbHocSinh.SelectedValue = dgvPhanLop.Rows[r].Cells[3].Value.ToString();

            cbHocSinh.Enabled = false;

            Utility.MaLop = cbLop.SelectedValue.ToString();
            EnableButton(true);
            btnSua.Visible = false;
            TrangThai = Status_QuanLiPhanLop.Update;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            EnableButton(true);
            btnThem.Visible = false;
            TrangThai = Status_QuanLiPhanLop.Insert;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
          
            if (TrangThai == Status_QuanLiPhanLop.Insert)
            {
                if (phanlop.ThemPhanLop(LayThongPhanLop()) == true)
                {

                   EnableButton(false);
                   FrmPhanLop_Load(sender, e);

                    dgvPhanLop.DataSource = phanlop.LayDanhSachPhanLop(cbNamHoc.SelectedValue.ToString(), cbKhoi.SelectedValue.ToString());

                }
            }
            if (TrangThai == Status_QuanLiPhanLop.Update)
            {
                if (phanlop.SuaPhanLop(LayThongPhanLop()) == true)
                {
                    
                    EnableButton(false);
                    dgvPhanLop.DataSource = phanlop.LayDanhSachPhanLop(cbNamHoc.SelectedValue.ToString(), cbKhoi.SelectedValue.ToString());

                }
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            EnableButton(false);
        }
        private PHANLOP LayThongPhanLop()
        {
            PHANLOP hs = new PHANLOP();
            hs.MaNamHoc = cbNamHoc.SelectedValue.ToString();
            hs.MaKhoi = cbKhoi.SelectedValue.ToString();
            hs.MaLop = cbLop.SelectedValue.ToString();
            hs.MaHocSinh = cbHocSinh.SelectedValue.ToString();

            return hs;
        }
        private void FrmPhanLop_Load(object sender, EventArgs e)
        {
          

            cbHocSinh.DataSource = hocsinh.LayDanhSachHocSinhChuaPhanLop();
            cbHocSinh.DisplayMember = "TenHocSinh";
            cbHocSinh.ValueMember = "MaHocSinh";

            cbKhoi.DataSource = khoi.LayDanhSachKhoi();
            cbKhoi.DisplayMember = "TenKhoi";
            cbKhoi.ValueMember = "MaKhoi";

            cbNamHoc.DataSource = namhoc.LayDanhSachNamHoc();
            cbNamHoc.DisplayMember = "TenNamHoc";
            cbNamHoc.ValueMember = "MaNamHoc";


            EnableButton(false);
        }

        private void lbMaLop_Click(object sender, EventArgs e)
        {

        }

        private void btnchon_Click(object sender, EventArgs e)
        {
            //bangdiem.KhoiTaoBangDiem("2013", "L002", "HS001");
            //MessageBox.Show(Utility.quidinh.SiSo);
            cbLop.DataSource = lop.LayDanhSachLopHocTheoNam(cbNamHoc.SelectedValue.ToString(), cbKhoi.SelectedValue.ToString());
            dgvPhanLop.DataSource = phanlop.LayDanhSachPhanLop(cbNamHoc.SelectedValue.ToString(), cbKhoi.SelectedValue.ToString());
            cbLop.DisplayMember = "TenLop";
            cbLop.ValueMember = "MaLop";
        }
    }
}
